﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.IO;

public partial class Employee_HRChangeInterview : System.Web.UI.Page
{
    int InterviewID;
    InterviewMethord IM = new InterviewMethord();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            txtDateInterview_CalendarExtender.StartDate = DateTime.Now;
            InterviewID = Convert.ToInt32(Request.QueryString["ID"].ToString());
            txtDateInterview_CalendarExtender.StartDate = DateTime.Now;
            if (txtInterviewID.Text.Equals(""))
            {
                LoadData(InterviewID);
            }
        }
        catch (Exception ex)
        {
            try
            {
                InterviewID = Convert.ToInt32(Request.QueryString["IDO"].ToString());
                LoadMessage();
            }
            catch (Exception exe)
            {
            }
        }
    }
    public void LoadMessage()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("~/ProposedInterview/" + Session["User"].ToString() + "_" + InterviewID + ".xml"));
        DataTable tb = new DataTable();
        tb = ds.Tables[0];
        if (tb.Rows.Count > 0)
        {
            txtInterviewID.Text = InterviewID.ToString();
            LoadInterviewerID(tb.Rows[0]["InterviewerID"].ToString());
            LoadVacancyID(tb.Rows[0]["VacancyID"].ToString());
            LoadApplicantID(tb.Rows[0]["ApplicantID"].ToString());
            txtDateInterview.Text = tb.Rows[0]["DateInterview"].ToString();
            txtTimeDetail.Text = tb.Rows[0]["TimeDetails"].ToString();
            ddlStatus.SelectedIndex = 1;
        }
        
    }
    private void LoadData(int _InterviewID)
    {
        try
        {
            DataTable tb = IM.ReturnInfomationInterview(_InterviewID);
            if (tb.Rows.Count > 0)
            {
                DataRow item = tb.Rows[0];
                txtInterviewID.Text = tb.Rows[0]["InterviewID"].ToString();
                LoadInterviewerID(item["InterviewerID"].ToString());
                LoadVacancyID(item["VacancyID"].ToString());
                LoadApplicantID(item["ApplicantID"].ToString());
                txtDateInterview.Text = item["DateInterview"].ToString().Substring(0, item["DateInterview"].ToString().IndexOf(" "));
                txtTimeDetail.Text = item["TimeDetails"].ToString();
                switch (Convert.ToInt32(tb.Rows[0]["Status"].ToString()))
                {
                    case 1:
                        ddlStatus.SelectedIndex = 0;
                        break;
                    case 2:
                        ddlStatus.SelectedIndex = 1;
                        break;
                    case 3:
                        ddlStatus.SelectedIndex = 2;
                        break;
                }
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
    }
    private void LoadInterviewerID(string _InterviewerID)
    {
        SqlDataReader dr = IM.ReturnInterviewer();
        ddlInterviewerID.DataSource = dr;
        ddlInterviewerID.DataTextField = "FullName";
        ddlInterviewerID.DataValueField = "InterviewerID";
        ddlInterviewerID.DataBind();
        IM.connClose();
        for (int i = 0; i < ddlInterviewerID.Items.Count; i++)
        {
            ddlInterviewerID.SelectedIndex = i;
            if (ddlInterviewerID.SelectedValue.Equals(_InterviewerID))
            {
                break;
            }
        }
    }
    private void LoadVacancyID(string _VacancyID)
    {
        SqlDataReader dr = IM.ReturnVacancy();
        ddlVacancyID.DataSource = dr;
        ddlVacancyID.DataTextField = "Title";
        ddlVacancyID.DataValueField = "VacancyID";
        ddlVacancyID.DataBind();
        IM.connClose();
        for (int i = 0; i < ddlVacancyID.Items.Count; i++)
        {
            ddlVacancyID.SelectedIndex = i;
            if (ddlVacancyID.SelectedValue.Equals(_VacancyID))
            {
                break;
            }
        }
    }
    private void LoadApplicantID(string _ApplicantID)
    {
        SqlDataReader dr = IM.ReturnApplicantID();
        ddlApplicantID.DataSource = dr;
        ddlApplicantID.DataTextField = "FullName";
        ddlApplicantID.DataValueField = "ApplicantID";
        ddlApplicantID.DataBind();
        IM.connClose();
        for (int i = 0; i < ddlApplicantID.Items.Count; i++)
        {
            ddlApplicantID.SelectedIndex = i;
            if (ddlApplicantID.SelectedValue.Equals(_ApplicantID))
            {
                break;
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        lblSuccess.Text = "";
        lblError.Text = "";
        if(txtTimeDetail.Text.Equals(""))
        {
            lblError.Text = "Please enter time detail";
            lblSuccess.Text = "";
            txtTimeDetail.Focus();
        }
        else if (txtDateInterview.Text.Equals(""))
        {
            lblError.Text = "Please choose date interview";
            lblSuccess.Text = "";
            txtDateInterview.Focus();
        }
        else
        {
            DateTime date = new DateTime();
            date = Convert.ToDateTime(txtDateInterview.Text);
            int stt = Convert.ToInt32(ddlStatus.SelectedItem.Value);
            bool result = IM.UpdateInterview(Convert.ToInt32(txtInterviewID.Text), Convert.ToString(ddlInterviewerID.SelectedValue), Convert.ToString(ddlVacancyID.SelectedValue), Convert.ToString(ddlApplicantID.SelectedValue), date, txtTimeDetail.Text, stt);
            if (result)
            {
                try
                {
                    InterviewID = Convert.ToInt32(Request.QueryString["IDO"].ToString());
                    File.Delete(Server.MapPath("~/ProposedInterview/" + Session["User"].ToString() + "_" + InterviewID + ".xml"));
                }
                catch (Exception ex)
                {
                }
                LoadData(InterviewID);
                if (ddlStatus.SelectedItem.ToString().Equals("Accept"))
                {
                    try
                    {
                        MailMessage msg = new MailMessage();
                        msg.From = new MailAddress("eprojectgroup1sem3@gmail.com");
                        msg.To.Add(IM.ReturnEmail(ddlApplicantID.SelectedValue));
                        msg.Subject = "Notice of the time of interview ABC company";
                        msg.Body = "  Hi! Application for your consideration we has been inspected. Appointment you right " + txtTimeDetail.Text + " on " + txtDateInterview.Text + " to the company's headquarters to participate in interviews appointment.";
                        SmtpClient sc = new SmtpClient("smtp.gmail.com");
                        sc.Port = 25;
                        sc.Credentials = new NetworkCredential("eprojectgroup1sem3@gmail.com", "admin!@#$%^");
                        sc.EnableSsl = true;
                        sc.Send(msg);
                        lblSuccess.Text = "Update success and email interview appointment has been sented";
                    }
                    catch (Exception ex)
                    {
                    }
                }
                else
                {
                    lblSuccess.Text = "Update Success!";
                }
            }
            else
            {
                lblError.Text = "Update faild";
            }
        }
    }
}